// BCDoctorWritePage.cpp : implementation file
//

#include "stdafx.h"
#include "../Frame/GYWalkAidApp.h"
#include "BCDoctorWritePage.h"
#include "../Common/CommonFun.h"
#include "../Common/GYDataSet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// BCDoctorWritePage dialog

static INPUT_FIELD_INFO s_InputFieldArray[] = 
{
	{NULL,		IBT_NoneType,	FALSE,	FALSE,	_T("10"),		ftString,	FALSE,	1000,		NULL,		NULL	}
};


CBCDoctorWritePage::CBCDoctorWritePage(CWnd* pParent, BOOL bBigRes) : CScrollPage(bBigRes ? CBCDoctorWritePage::IDD : CBCDoctorWritePage::SD_IDD, pParent)
, m_edtDoctWrite(&m_InputFieldArray[0])
{
	//{{AFX_DATA_INIT(BCDoctorWritePage)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	LPVOID pCtrl = NULL;
	DWORD dwControlType = IBT_NoneType;
	for(int i = 0; i < INPUT_EDIT_CTRL_COUNT; i++)
	{
		pCtrl = m_InputFieldArray[i].pLinkCtrl;
		dwControlType = m_InputFieldArray[i].dwCtrlType;
		memcpy(&m_InputFieldArray[i], &s_InputFieldArray[i], sizeof(INPUT_FIELD_INFO));
		m_InputFieldArray[i].pLinkCtrl = pCtrl;
		m_InputFieldArray[i].dwCtrlType = dwControlType;
	}
}


void CBCDoctorWritePage::DoDataExchange(CDataExchange* pDX)
{
	CScrollPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(BCDoctorWritePage)
	DDX_Control(pDX, IDC_EDIT_DOCTOR_WRITE, m_edtDoctWrite);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBCDoctorWritePage, CScrollPage)
	//{{AFX_MSG_MAP(BCDoctorWritePage)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// BCDoctorWritePage message handlers
void CBCDoctorWritePage::OnSize(UINT nType, int cx, int cy) 
{
	CScrollPage::OnSize(nType, cx, cy);
	
	//...
}

BOOL CBCDoctorWritePage::ClearData()
{
	m_edtDoctWrite.SetWindowText(_T(""));
	return TRUE;
}

int CBCDoctorWritePage::SaveData(CString strMRN, int nDiagSN, BOOL bIsDraft)
{
	if(strMRN.IsEmpty())
	{
		GYMsgBox(IDS_PAT_MRN_EMPTY, MB_OK|MB_ICONSTOP);
		return SaveBCE_Other;
	}

	//  Reg the BodyCheck this time
	CTime curTime = CTime::GetCurrentTime();
	CString strCheckDate;
	strCheckDate.Format(_T("%4d-%02d-%02d"), curTime.GetYear(), curTime.GetMonth(), curTime.GetDay());
	
	CGYDataSet bc;
	
	//  The body check record exists?
	CString strSQL;
	strSQL.Format(_T("SELECT BCID, COUNT(BCID) AS RecCnt FROM BodyCheckReg WHERE MedicalRecordNo='%s' AND DiagSN=%d AND CheckDate='%s' AND ScaleID=%d"), strMRN, nDiagSN, strCheckDate, BODYCHECKSCALEID);
	bc.OpenTable(strSQL);

	CString strRecCnt;
	bc.GetFieldValue(_T("RecCnt"), strRecCnt);
	int nRecCnt = _ttoi(strRecCnt);
	if(nRecCnt > 1)
	{
		CString strFormat = GetResString(IDS_GBL_DATA_DUPLICATE);
		CString strError;
		strError.Format(strFormat, strSQL);
		GYMsgBox(strError, MB_OK|MB_ICONERROR);
		return SaveBCE_Database;
	}

	BOOL bExistRecord = FALSE;
	CString strBCID;
	if(nRecCnt == 1)
	{
		bExistRecord = TRUE;
		
		bc.GetFieldValue(_T("BCID"), strBCID);
		strSQL.Format(_T("UPDATE BodyCheckReg SET IsDraft=%d WHERE BCID=%s"), bIsDraft, strBCID);
		bc.ExecuteSQL(strSQL);
	}
	else
	{
		//  Start transation
		bc.BeginTrans();
		strSQL.Format(_T("INSERT INTO BodyCheckReg (MedicalRecordNo, DiagSN, CheckDate, ScaleID, IsDraft) VALUES ('%s', %d, '%s', %d, %d)"), strMRN, nDiagSN, strCheckDate, BODYCHECKSCALEID, bIsDraft);
		BOOL bRet = bc.ExecuteSQL(strSQL);
		if(!bRet)
		{
			bc.Rollback();
			DataErrorMsgBox(__FILE__, __LINE__, strSQL, bc.GetLastErrorString());
			return SaveBCE_Database;
		}

		strSQL.Format(_T("SELECT BCID, COUNT(BCID) AS RecCnt FROM BodyCheckReg WHERE MedicalRecordNo='%s' AND DiagSN=%d AND CheckDate='%s' AND ScaleID=%d"), strMRN, nDiagSN, strCheckDate, BODYCHECKSCALEID);
		bc.OpenTable(strSQL);
		bc.GetFieldValue(_T("BCID"), strBCID);

		//  Commit transation
		bc.CommitTrans();
	}

	//  Save doctor-write scale, even it's blank

	BOOL bSQLRet = TRUE;
	CString strText = m_edtDoctWrite.GetControlData(FALSE);
		
	if(bExistRecord)
	{
		strSQL.Format(_T("UPDATE BodyCheckDoctWrite SET FieldID=%s, FieldValue='%s' WHERE BCID=%s"),
			m_InputFieldArray[0].szFieldName, strText, strBCID);
	}
	else
	{
		strSQL.Format(_T("INSERT INTO BodyCheckDoctWrite (BCID, FieldID, FieldValue) VALUES (%s, %s, '%s')"),
			strBCID, m_InputFieldArray[0].szFieldName, strText);
	}

	bSQLRet = bc.ExecuteSQL(strSQL);
	return bSQLRet ? SaveBCE_Succ : SaveBCE_Database;
}

BOOL CBCDoctorWritePage::ShowTableDataWithStartCol(CString strMRN, int nDiagSN, int nStartCol)
{
	return TRUE;
}

BOOL CBCDoctorWritePage::SetLastColTitile(CString strDate, int nDiagSN)
{
	return TRUE;
}

BOOL CBCDoctorWritePage::ShowTableData(CString strMRN, int nDiagSN)
{
	CString strSQL;
	strSQL.Format(_T("SELECT CheckDate, BCID, IsDraft FROM BodyCheckReg WHERE MedicalRecordNo='%s' AND DiagSN=%d AND ScaleID=%d ORDER BY CheckDate DESC LIMIT 1"), strMRN, nDiagSN, BODYCHECKSCALEID);
	
	CString strCheckDate;
	CString strBCID;
	CString strIsDraft;
	
	CGYDataSet tblDoctWrite;
	tblDoctWrite.OpenTable(strSQL);
	if(tblDoctWrite.IsEOF())
		return TRUE;
	
	tblDoctWrite.GetFieldValue(_T("CheckDate"), strCheckDate);
	tblDoctWrite.GetFieldValue(_T("BCID"), strBCID);
	tblDoctWrite.GetFieldValue(_T("IsDraft"), strIsDraft);
	tblDoctWrite.Close();
	
	if(strBCID.IsEmpty())
		return TRUE;

	BOOL bIsDraft = _ttoi(strIsDraft);
	
	strSQL.Format(_T("SELECT FieldID, FieldValue FROM BodyCheckDoctWrite WHERE BCID=%s ORDER BY FieldID"), strBCID);
	
	CGYDataSet tblBCResult;
	tblBCResult.OpenTable(strSQL);
	if(tblBCResult.IsEOF())
		return TRUE;
	
#ifdef _DEBUG
	CString strFieldID;
	tblBCResult.GetFieldValue(_T("FieldID"), strFieldID);
	ASSERT(strFieldID == m_InputFieldArray[0].szFieldName);
#endif

	CString strFieldValue;
	tblBCResult.GetFieldValue(_T("FieldValue"), strFieldValue);
	
	CLinkEdit* pEdit = (CLinkEdit*)m_InputFieldArray[0].pLinkCtrl;
	if(pEdit)
	{
		pEdit->SetWindowText(strFieldValue);
		
		if(m_InputFieldArray[0].bAlwaysReadOnly)
			pEdit->SetReadOnly(TRUE);
		else
			pEdit->SetReadOnly(!bIsDraft);
	}

	return bIsDraft;
}

BOOL CBCDoctorWritePage::EnableInputControls(BOOL bEnable)
{
	m_edtDoctWrite.SetReadOnly(!bEnable);

	return TRUE;
}

BOOL CBCDoctorWritePage::IsPageCurDayBlank()
{
	CString strText = m_edtDoctWrite.GetControlData(FALSE);
	return strText.IsEmpty();	
}
